<?php
/**
 * This file contains function definitions related to the portfolio post type 
 * 
 * @author Mike Burns
 */

// Requires
require( 'portfolio-template-tags.php' );
require( 'portfolio-widget.php' );

// Register a portfolio widget (see portfolio-widgets.php)
add_action( 'widgets_init', 'cdia_port_register_portfolio_widget' );

// Define our portfolio shortcode [portfolio] 
add_shortcode( 'portfolio', 'cdia_port_portfolio_shortcode' );

/**
 *  Register portfolio post type
 */
function cdia_port_register_portfolio() {

	// Post features supported by our custom post type
	$supports = array(		// What features does this post type support?
		'title',		// - Post title
		'editor',		// - Post content editor
		'thumbnail'		// - Featured image
	    );

	// Labels for various administrative views of our custom post type
	$labels =  array(
		'name' => 'Portfolios',
		'singular_name' => 'Portfolio',
		'all_items' => 'All Portfolios',
		'add_new' => 'Add New',
		'add_new_item' => 'Add New Portfolio',
		'edit_item' => 'Edit Portfolio',
		'new_item' => 'New Portfolio',
		'view_item' => 'View Portfolio',
		'search_items' => 'Search Portfolios',
		'not_found' => 'No Portfolios Found',
		'not_found_in_trash' => 'No Portfolios Found In Trash'
	    );
	
	// Set up portfolio arguments
	$portfolio_args = array(
	    'public' => true,			// Show in admin interface
	    'query_var' => 'portfolio',		// For querying database
	    'rewrite' => array(			
		'slug' => 'portfolio',		// URL slug for portfolio items (single-portfolio.php)
	    ),
	    'has_archive' => true,		// Has an archive
	    'supports' => $supports,
	    'labels' => $labels
	);

	// Register post type
	register_post_type( 'portfolio', $portfolio_args );
	
}

/**
 * Define a custom portfolio shortcode.
 * 
 * Our shortcode has the following attributes:
 * 	- count (int) (-1 for all, or postivie integer)
 * 		- how many portfolio items to show
 * 	- show_title (boolean) (true|false)
 * 		- display the portfolio item title?
 * 	- show_description (boolean) (true|false)
 * 		- display the portfolio item description?
 * 	- show_thumb (boolean) (true|false) 
 * 		- display the portfolio item thumbnail?
 * 
 * @global StdObj $post A WordPress StdObj for the current post
 * @param array $atts The user-defined shortcode attributes if they exist
 * 
 * @return string Resulting output 
 */
function cdia_port_portfolio_shortcode( $atts ) {
	global $post;

	// Start output buffering
	// This re-routes content that we echo below to an output buffer,
	// which we will grab later using ob_get_contents()
	ob_start();

	// Our default portfolio shortcode attributes
	$default_atts = array(
	    'count' => 3,
	    'show_title' => true,
	    'show_description' => true,
	    'show_thumb' => true,
	);

	// Merge defaults with input
	$args = shortcode_atts( $default_atts, $atts );

	// Query arguments to fetch our portfolio posts
	$query_args = array(
	    'posts_per_page' => $args['count'],
	    'post_type' => 'portfolio',
	    'post_status' => 'publish'
	);
	
	// Fetch posts using the powerful WP_Query class
	// See http://codex.wordpress.org/Class_Reference/WP_Query for details
	$portfolio_query = new WP_Query( $query_args );

	// Loop through posts
	if( $portfolio_query->have_posts() ) {

		while( $portfolio_query->have_posts() ) {
			
			// Setup post data for the current post:
			// This allows us to use loop template tags like the_title() and the_content() below
			$portfolio_query->the_post();
			
			// Show portfolio item title and thumbnail, only if the shortcode attributes have been defined
			if( $args['show_title'] ) the_title( '<h2>', '</h2>');
			if( $args['show_thumb'] ) the_post_thumbnail( 'medium', array('class' => 'alignright' ));

			// Render this portfolio post item
?>
			<dl class="portfolio-details">
				<dt>Client Name</dt><dd><?php cdia_the_portfolio_client(); ?></dd>
				<dt>Completion Year</dt><dd><?php cdia_the_portfolio_year(); ?></dd>
				<dt>Site URL</dt><dd><?php cdia_the_portfolio_site_url(); ?></dd>
				<dt>CMS</dt><dd><?php cdia_the_portfolio_cms(); ?></dd>
			</dl>
<?php
			// Show the portfolio item description if our shortcode is asking for it
			if( $args['show_description'] ) the_content( '<div class="description">', '</div>');
		}
		
	}
	
	// Reset posts for rest of post display
 	wp_reset_postdata();

	// Grab output buffer contents
	$output = ob_get_contents();
	
	// Clean output buffer
	ob_end_clean();
	
	// Return our content
	return $output;
}

?>